# Date：2019/1/17
# Name: 好威
# -*- coding:utf-8 -*-
import pymysql as mysql
import configparser


class OperationMysql(object):
    def __init__(self):
        config = configparser.ConfigParser()
        # read(filename)  # 读取配置文件，直接读取ini文件内容
        config.read(r'D:\commercial_opportunity_zhu\config\config.ini')
        host = config.get('db', 'db_host')
        user = config.get('db', 'db_user')
        pwd = config.get('db', 'db_pass')
        name = config.get('db', 'db_name')
        port = eval(config.get('db', 'db_port'))
        # 打开数据库连接
        self.db = mysql.connect(host=host, user=user, port=port, password=pwd, database=name)
        print("数据库开始")

    def db_select(self, sql):
        try:
            cs1 = self.db.cursor()
            count = cs1.execute(sql)
            print("查询到%d条数据:" % count)
            for i in range(count):
                result = cs1.fetchone()
                print(result)

        except BaseException as e:
            print(e)

    def db_close(self):
        try:
            self.db.close()
            print("关闭数据库")
        except BaseException as e:
            print(e)


if __name__ == '__main__':
    a = OperationMysql()
    # sql = 'select * from students;'
    # a.db_select(sql)
    a.db_close()
